/**
 * HTML操作工具(HTMLUtils)
 * 
 * { 方法参数,dom:选择元素字符,如:id:'#id',class:'.class',attribute:'[attribute]|[attribute=attributeValue]' }
 * 
 * { domNumChange:DOM数字变化,_change_num:变化数, _operate:数学运算符,加:'+',减:'-',乘:'*',除:'/' }
 * 
 * { domContentDelete:延时删除DOM内容,_time:时间,毫秒 }
 * 
 * { domAttributeValues:获取元素所有的属性值,返回JSON对象,直接通过属性名访问(使用'_'替换'-'),
 *     attributeValue=domAttributeValues(dom).attributeName }
 * 
 * { domToggle:显示隐藏,event:绑定事件,zone:事件触发区域,target:显示隐藏区域 }
 * 
 * { domAutoBindEvent:DOM自动绑定同一个事件,event:事件,callback:事件处理函数 }
 * 
 * 
 */

(function(w) {
	if (!w)
		return;
	var HTMLUtils = function() {

	};
	HTMLUtils.prototype = {
			
		__getType : function(_obj) {
			return typeof (_obj);
		},
		__getDom : function(_dom) {
			return document.querySelector(_dom);
		},
		__getDomById : function(_dom_id) {
			return document.getElementById(_dom_id);
		},
		__getDoms : function(_dom) {
			return document.querySelectorAll(_dom);
		},
		__getDomsLength : function(doms) {
			return doms.length;
		},
		domNumChange : function(dom, _change_num, _operate) {
			var _dom = this.__getDom(dom), _current_num = _dom.innerHTML, _operate_str = _current_num+ _operate + _change_num;
				_dom.innerHTML = eval(_operate_str);
		},
		domContentDelete : function(dom, _time) {
			var _dom = this.__getDom(dom), __time = _time || 3000;
				setTimeout(function(dom) {
					_dom.innerHTML = "";
				}, __time);
		},
		domAttributeValues : function(dom) {
			var _dom = this.__getDom(dom), _attributes = _dom.attributes, _length = _attributes.length, _json = {};
			for (var i = 0; i < _length; i++) {
				_json[_attributes[i].nodeName.replace(/-/g, '_')] = _attributes[i].nodeValue;
			}

			return _json;
		},
		domToggle : function(event, zone, target) {
			var _zone = this.__getDom(zone), _target = this.__getDom(target);
			if (_zone) {
				_zone.style.cursor = 'pointer';
				_zone.addEventListener(event, function(evt) {
					var _display = _target.style.display;
					if (_display == 'none') {
						_target.style.display = 'block';
					} else if (_display == 'block') {
						_target.style.display = 'none';
					}
				});
			} else {
				console.error('HTMLUtils domToggle ERROR,zone:' + zone+ ' can not found!');
			}

		},
		commonBindEvent : function(dom, event, callback) {
			if (dom.addEventListener) {
				dom.addEventListener(event, callback, false);
			} else if (dom.attachEvent) {
				dom.attachEvent("on" + event, callback);
			}
		},
		domAutoBindEvent : function(dom, event, callback) {
			var _doms = this.__getDoms(dom), _length = this
					.__getDomsLength(_doms);
			if (_doms) {
				for (var i = 0; i < _length; i++) {
					var _dom = _doms[i];
					this.commonBindEvent(_dom, event, callback);
				}
			}

		},
		domAutoBindEventById : function(domid, event, callback) {
			var _dom = this.__getDomById(domid);
			this.commonBindEvent(_dom, event, callback);
		},
		isBlank : function() {
			var _str = arguments[0] || "";
			return _str.length == 0 || _str == ""
					|| _str.replace(/(^\s*)|(\s*$)/g, "") == "";
		},
		isNotBlank : function() {
			var _str = arguments[0] || "";
			return !this.isBlank(_str);
		},
		isNum : function() {
			var _num = arguments[0] || "";
			
			if(this.isBlank(_num)){
				return false;
			}
			
			if (isNaN(_num)) {
				return false;
			} else {
				return true;
			}

		}
	};
	
	w.HTMLUtils = new HTMLUtils();
})(window);
